﻿using Castle.Core.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;

namespace VOL.Core.Utilities
{
    public class DjlcGetMaxCode
    {
        public static cw_djlc_bomnumber_view GetMaxCode()
        {
            // 获取当前的最大的序号、创建的时候给流程编号赋值
            /**
             * cw_djlc_bomnumber_view 中包含 当天最大的序号及当天最大的成立编号
             */
            cw_djlc_bomnumber_view cwDjlcBomnumberView = new cw_djlc_bomnumber_view();
            string sqlString = "select bomnumber,cw_djlc_code from cw_djlc_bomnumber_view";
            string newbomnumber = null;
            string cwDjlcCode = null;
            // List<string> list = DBServerProvider.SqlDapper.QueryList<string>(sqlString, null).ToList();
            List<cw_djlc_bomnumber_view> list = DBServerProvider.SqlDapper.QueryList<cw_djlc_bomnumber_view>(sqlString, null).ToList();

            foreach (cw_djlc_bomnumber_view cw_djlc_bomnumber_view in list)
            {
                if (cw_djlc_bomnumber_view.bomnumber.IsNullOrEmpty())
                {
                    newbomnumber = DateTime.Now.ToString("yyyyMMdd") + "_01";
                }
                else
                {
                    // 查询到的最大物料编号长度
                    int length = cw_djlc_bomnumber_view.bomnumber.Length;
                    // 最大物料编号
                    var bomnumber = cw_djlc_bomnumber_view.bomnumber;
                    // 最大物料编号对应的序号
                    int newbomnumberCount = bomnumber.Substring(9, length - 9).ToInt() + 1;

                    if (newbomnumberCount < 10)
                    {
                        newbomnumber = bomnumber.Substring(0, 8) + "_0" + newbomnumberCount;
                    }
                    else
                    {
                        newbomnumber = bomnumber.Substring(0, 8) + "_" + newbomnumberCount;
                    }
                }
                cwDjlcBomnumberView.bomnumber = newbomnumber;
                if (cw_djlc_bomnumber_view.cw_djlc_code.IsNullOrEmpty())
                {
                    cwDjlcCode = "cw_djlc_" + DateTime.Now.ToString("yyyyMMdd") + "_01";
                }
                else
                {
                    // 查询到的定价流程编号长度
                    int length = cw_djlc_bomnumber_view.cw_djlc_code.Length;
                    // 定价流程编号
                    var cw_djlc_code = cw_djlc_bomnumber_view.cw_djlc_code;
                    // 定价流程对应的序号
                    int cwDjlcCodeCount = cw_djlc_code.Substring(17, length - 17).ToInt() + 1;
                    if (cwDjlcCodeCount < 10)
                    {
                        cwDjlcCode = cw_djlc_code.Substring(0, 17) + "0" + cwDjlcCodeCount;
                    }
                    else
                    {
                        cwDjlcCode = cw_djlc_code.Substring(0, 17)  + cwDjlcCodeCount;
                    }
                }
                cwDjlcBomnumberView.cw_djlc_code = cwDjlcCode;
            }
            return cwDjlcBomnumberView;
        }
    }
}
